package com.cychat.mall.controller;

import com.Cychat.commons.entity.ResultMessage;
import com.Cychat.commons.entity.pojo.Product;
import com.Cychat.commons.entity.pojo.Review;
import com.Cychat.commons.entity.pojo.User;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cychat.mall.entity.DTO.ReviewDTO;
import com.cychat.mall.service.ProductService;
import com.cychat.mall.service.ReviewService;
import com.cychat.mall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Auther: 刘俊  <br/>
 * Date: 2025-10-30:下午 2:17  <br/>
 * Description: 商品控制器
 */
@RestController
@RequestMapping("/api/mall")
public class ProductController {
    @Autowired
    ProductService productService;
    @Autowired
    ReviewService reviewService;
    @Autowired
    UserService userService;

    @GetMapping("/product/list")
    public ResultMessage<List<Product>> list(@RequestParam(value = "categoryId", required = false) Integer categoryId) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();

        if (categoryId != null) {
            wrapper.eq(Product::getCategoryId, categoryId)
                    .eq(Product::getStatus, 1)     //在售状态
                    .gt(Product::getStock, 0);     // 库存大于0
        }
        // 如果 categoryId 为 null，就不加条件，查所有

        List<Product> products = productService.list(wrapper);
        return ResultMessage.success(products);
    }

    //根据id查询商品详情
    @GetMapping("/product/detail")
    public ResultMessage<Product> detail(@RequestParam("id") Integer id) {
        Product product = productService.getById(id);
        return ResultMessage.success(product);
    }

    //根据商品id查询用户评价
    @GetMapping("/product/review")
    public ResultMessage<List<ReviewDTO>> review(@RequestParam("id") Integer id) {
        List<Review> reviews = reviewService.list(new LambdaQueryWrapper<Review>()
                .eq(Review::getProductId, id));
        // 2. 转换为 ReviewDTO 列表
        List<ReviewDTO> reviewDTOs = reviews.stream()
                .map(review -> {
                    ReviewDTO dto = new ReviewDTO();
                    dto.setReviewId(review.getReviewId());
                    dto.setProductId(review.getProductId());
                    dto.setUserId(review.getUserId());
                    dto.setGrade(review.getGrade());
                    dto.setDescription(review.getDescription());
                    dto.setCreateTime(review.getCreateTime());
                    dto.setUpdateTime(review.getUpdateTime());
                    // 3. 设置昵称和头像URL（根据 userId 查询用户信息）
                    // UserService 来查询用户信息
                     User user = userService.getById(review.getUserId());
                     dto.setNickname(user.getNickName());
                     dto.setAvatarUrl(user.getAvatarUrl());
                    return dto;
                })
                .collect(Collectors.toList());
        return ResultMessage.success(reviewDTOs);
    }

}
